<?php


namespace App\Admin\Controller\App;


use App\Admin\Controller\AdminBaseController;
use App\Admin\Requests\App\Project\ProjectStoreRequest;
use App\Admin\Requests\App\Project\ProjectUpdateAuditItemListRequest;
use App\Admin\Requests\App\Project\ProjectUpdateDomainRequest;
use App\Admin\Requests\App\Project\ProjectUpdateRequest;
use App\Admin\Services\App\ProjectService;

class ProjectController extends AdminBaseController {
    /**
     * @param ProjectService $service
     *
     * @return mixed
     */
    public function page(ProjectService $service) {
        return $this->success($service->getProjectPage());
    }

    /**
     * 获取应用授权页面url
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     */
    public function getAuthLoginPageUrl(ProjectService $service, $id) {
        return $this->success($service->getAuthLoginPageUrl($id));
    }

    /**
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function publishMiniProgram(ProjectService $service, $id){
        return $this->success($service->publishMiniProgram($id));
    }

    /**
     * @param ProjectService $service
     * @param ProjectStoreRequest $request
     * @return mixed
     * @throws \Exception
     */
    public function store(ProjectService $service, ProjectStoreRequest $request) {
        return $this->success($service->projectStore($request->post(),$request->getRuleKeys()));
    }

    /**
     * @param ProjectService       $service
     * @param ProjectUpdateRequest $request
     * @param                      $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function update(ProjectService $service, ProjectUpdateRequest $request, $id) {
        return $this->success($service->projectUpdate($id, $request->input(), $request->getRuleKeys()));
    }

    /**
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function destroy(ProjectService $service, $id) {
        return $this->success($service->projectDelete($id));
    }

    /**
     * 获取体验版二维码
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return \EasyWeChat\Kernel\Http\Response|mixed
     * @throws \Exception
     */
    public function getExperienceQrCode(ProjectService $service, $id){
        return $service->getExperienceQrCode($id);
    }

    /**
     * 修改代码模板的服务器域名及业务域名
     *
     * @param ProjectService $service
     * @param ProjectUpdateDomainRequest $request
     * @param $id
     * @return mixed
     * @throws \Exception
     */
    public function updateDomain(ProjectService $service, ProjectUpdateDomainRequest $request, $id) {
        return $this->success($service->updateDomain($id, $request->input(), $request->getRuleKeys()));
    }

    /**
     * 同步服务器域名配置
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function synServerDomain(ProjectService $service, $id) {
        return $this->success($service->synServerDomain($id));
    }

    /**
     * 同步业务域名配置
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function synWebviewDomain(ProjectService $service, $id) {
        return $this->success($service->synWebviewDomain($id));
    }

    /**
     * 修改审核项列表
     *
     * @param ProjectService $service
     * @param $id
     * @param ProjectUpdateAuditItemListRequest $request
     * @return mixed
     * @throws \Exception
     */
    public function updateAuditItemList(ProjectService $service, $id, ProjectUpdateAuditItemListRequest $request) {
        return $this->success($service->updateAuditItemList($id, $request));
    }

    /**
     * 版本回退
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function revertCodeRelease(ProjectService $service, $id) {
        return $this->success($service->revertCodeRelease($id));
    }

    /**
     * 查询最新一次提交的审核状态
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function getLatestAuditStatus(ProjectService $service, $id) {
        return $this->success($service->getLatestAuditStatus($id));
    }

    /**
     * 上传小程序代码
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function codeCommit(ProjectService $service, $id) {
        return $this->success($service->codeCommit($id));
    }

    /**
     * 提交审核
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function miniProgramSubmitAudit(ProjectService $service, $id) {
        return $this->success($service->miniProgramSubmitAudit($id));
    }

    /**
     * 小程序审核撤回
     *
     * @param ProjectService $service
     * @param                $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function miniProgramWithdrawAudit(ProjectService $service, $id) {
        return $this->success($service->miniProgramWithdrawAudit($id));
    }

    /**
     * 发布已通过审核的小程序
     *
     * @param ProjectService $service
     * @param int            $id
     *
     * @return mixed
     * @throws \Exception
     */
    public function miniProgramRelease(ProjectService $service, int $id) {
        return $this->success($service->miniProgramRelease($id));
    }

    /**
     * 加急审核申请
     *
     * @param ProjectService $service
     * @param int            $id
     * @param                $auditId
     *
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     */
    public function speedUpAudit(ProjectService $service, int $id, $auditId) {
        //加急审核申请
        return $this->success($service->speedUpAudit($id, $auditId));
    }

    /**
     * 查询服务商的当月提审限额（quota）和加急次数
     *
     * @param ProjectService $service
     * @param int            $id
     *
     * @return mixed
     */
    public function getMonthSubmitLimitInfo(ProjectService $service, int $id) {
        return $this->success($service->getMonthSubmitLimitInfo($id));
    }

    /**
     * 查询小程序用户隐私保护指引
     * @param ProjectService $service
     * @param int $id
     * @return mixed
     * @throws \Exception
     */
    public function getPrivacy(ProjectService $service, int $id) {
        return $this->success($service->getPrivacy($id));
    }

    /**
     * 配置小程序用户隐私保护指引
     * @param ProjectService $service
     * @param int $id
     * @return mixed
     * @throws \EasyWeChat\Kernel\Exceptions\InvalidConfigException
     * @throws \GuzzleHttp\Exception\GuzzleException
     */
    public function setPrivacy(ProjectService $service, int $id)
    {
        $param = array(
            'owner_setting' => array('contact_email' => "lsq5017@163.com", 'notice_method' => "电话", "store_expire_timestamp" => "180天"),
            'setting_list' => [
                ['privacy_key' => 'UserInfo', 'privacy_text' => "用户信息（微信昵称、头像)"],
                ['privacy_key' => 'Location', 'privacy_text' => "位置信息"],
                ['privacy_key' => 'Address', 'privacy_text' => "地址"],
                ['privacy_key' => 'Record', 'privacy_text' => "麦克风"],
                ['privacy_key' => 'Album', 'privacy_text' => "选中的照片或视频信息"],
                ['privacy_key' => 'Camera', 'privacy_text' => "摄像头"],
                ['privacy_key' => 'PhoneNumber', 'privacy_text' => "手机号码"],
                ['privacy_key' => 'DeviceInfo', 'privacy_text' => "设备信息"],
                ['privacy_key' => 'EXIDNumber', 'privacy_text' => "身份证号码"],
            ],
            'privacy_ver' => 2
        );
        return $this->success($service->setPrivacy($id, $param));
    }
}
